The Jet engine is the database management system used by Visual Basic and Microsoft Access. The Jet engine is responsible for the retrieval and storage of data in users' databases. The Jet engine supplied with Visual Basic comes in the following three
forms:
The Jet engine is a collection of objects controlled by your application. The objects are used to access and manipulate information contained in a database used by your application. The collection of objects is referred to as Data Access Objects, or the
DAO layer. The following objects make up the DAO layer:
These objects—and the properties and methods associated with them—are covered in this chapter.
To ensure that the Data Access Objects are available to your project, select the appropriate DAO library (such as Microsoft DAO 3.0 Object Library ) from the Tools | References selection dialog box.
The Standard edition of Visual Basic contains a 32-bit version of the Jet engine. With the Standard edition, you cannot directly manipulate the DAO—you can only use the built-in Data control and data bound controls in order to manipulate data
stored in a Jet database.
The Professional edition of Visual Basic contains two versions of the Jet engine:
In the preceding chapter, you saw how the Data control allows you to quickly create an application to view or edit existing records. However, you cannot use the Data control to add, delete, or change many of the underlying properties of the recordset.
To make these changes, you must drop down to the DAO layer.
The Data Access Object (DAO) model is a hierarchy of objects and collections (see Figure 28.1). In order to access the information displayed in Figure 28.1, search the Help file supplied with Visual Basic for the topic Jet Database Engine.
Figure 28.1. The Data Access Object model is a hierarchy.
The following sections discuss each of the objects or collections that make up the Data Access Object model.
The DBEngine object is the top layer of the DAO. All other objects and collections are contained in the DBEngine object. Although you do not have to define the DBEngine in your application, you cannot have more than one instance of this object in your
application. The DBEngine object contains and defines the Workspace and Error objects and collections.
The following properties are available for use by the DBEngine object:
The following methods are available to the DBEngine:
Parameter |
Description |
Name |
The Name parameter defines the name of the new workspace. The name of each workspace must be unique. |
Userid |
The Userid parameter defines the user creating the workspace. |
Password |
The Password parameter defines the user's password. Userid and password are needed only for secured MS Access databases. |
The Workspace object allows you to open databases so that you can perform operations on the database object. You can have multiple workspaces in an application.
The Workspace object contains and defines the User, Group, and Database objects and collections.
The following properties are available to the Workspace object:
The following methods are available to the Workspace object:
In the following code example, the Notes field in the Titles table of the BIBLIO database is set to Test Notes when the Yes button is selected from the MsgBox that is displayed.
Dim wstitle As Workspace Dim dbbiblio As Database Dim rstitles As Recordset Set wstitle = DBEngine.CreateWorkspace("title", "Admin", "") Set dbbiblio = wstitle.OpenDatabase("c:\vb40\biblio.mdb", False, False) Set rstitles = dbbiblio.OpenRecordset(" select * from titles", dbOpenDynaset) If rstitles.RecordCount = 0 Then Exit Sub End If wstitle.BeginTrans Do While rstitles.EOF = False rstitles.Edit rstitles("notes") = "Test Notes " rstitles.Update rstitles.MoveNext Loop If MsgBox("Do you wish to Save the Changes you have Made ", vbYesNo, "", "", _0) = vbYes Then wstitle.CommitTrans Else wstitle.Rollback End If
Parameter |
Description |
Name |
Represents the name to be assigned to the new Group object. |
PID |
Represents the group account to be assigned to the new Group object. |
Parameter |
Description |
Name |
Represents the name to be assigned to the new User object. |
PID |
Represents the account to be assigned to the new User object. |
Password |
Represents the password to be used by the new User object. |
Parameter |
Description |
Dbname |
If the database is a Microsoft Access—type database, the Dbname property contains the name and the path to the database. If the database is an ODBC-type database, the Dbname property can either be a valid ODBC source name or can be left blank (so that a dialog box of existing ODBC sources will be displayed). If the database is a replaceable ISAM-type database, the directory in which the database files reside is specified. If the database to be opened is a Btreive-type database, the name of the database definition file is specified. |
Exclusive |
This parameter is used to open the database for exclusive use by your application. If the database is currently in use by another user, a trappable error is generated when you try to access it. |
Read-only |
This parameter is used to open the database so that the user cannot make changes to the data in the database. |
Connect |
The Connect property provides additional information needed to open ODBC-type databases. An example of additional information that may be needed for an ODBC database would be Datasource, Userid, and password. |
The Database object allows you to manipulate databases residing on your computer or on a network. The Database object contains and defines the TableDef, QueryDef, and Recordset objects and collections.
The following properties are available to the Database object/collection:
The following example checks whether or not transactions are allowed for a Database object. If they are, the actions performed on the Database object are part of a transaction block. Otherwise, the transaction block is ignored.
sub begintransaction (ws as workspace, db as database) if db.transaction= true then ws.BeginTrans endif end sub
The following example checks the Updatable property to determine whether or not an edit action can be completed:
Dim wstitle As Workspace Dim dbbiblio As Database Dim rstitles As Recordset Set wstitle = DBEngine.CreateWorkspace("title", "Admin", "") Set dbbiblio = wstitle.OpenDatabase("c:\vb40\biblio.mdb", False, True) Set rstitles = dbbiblio.OpenRecordset(" select * from titles", dbOpenDynaset) If dbbiblio.Updatable = True Then rstitles.Edit Else MsgBox ("This Database Object Is Read Only ") End If
The following example displays the version of the DBEngine and the version of the BIBLIO.MDB database. With the 16-bit version of Visual Basic, the version of the DBEngine should be 2.5 and the version of the BIBLIO.MDB database should be 2.0. With the
32-bit version of Visual Basic, the version of the DBEngine would be 3.0, and the version of the BIBLIO.MDB database would be unchanged.
Dim wstitle As Workspace Dim dbbiblio As Database Dim rstitles As Recordset Set wstitle = DBEngine.CreateWorkspace("title", "Admin", "") Set dbbiblio = wstitle.OpenDatabase("c:\vb40\biblio.mdb", False, True) MsgBox " The Version of the DBEngine is " _ & DBEngine.Version & ". The version of the " _ & dbbiblio.Name & " Database is " _ & dbbiblio.Version & "."
The following methods are available to the Database object/collection:
Parameter |
Description | |
Source |
The Source parameter indicates the contents of the recordset. The Source parameter can be a table contained in the database, a record-returning SQL statement, or an existing QueryDef contained in the Database object. | |
Type |
The Type parameter indicates the type of recordset you want to open. The following constants are available for use as the Type parameter: | |
|
dbOpenTable |
A table-type recordset is created. |
|
dbOpenDynaset |
A Dynaset-type recordset is created. |
|
dbOpenSnapShot |
A snapshot-type (Read-only Dynaset) recordset is created. |
|
The default type of recordset to open is a table-type recordset. | |
Options |
The Options parameter further defines the characteristics of the recordset you are creating. The following constants are available for use with the Options parameter (you can combine options by adding them together using the + sign between constants): | |
|
dbDenyWrite |
Other users cannot make any changes to the records contained in your recordset. |
|
dbDenyRead |
Other users cannot read any of the records contained in your recordset. This option applied only to recordsets created with the dbOpenTable type. |
|
dbReadOnly |
The recordset is opened as read-only and no changes can be made. |
|
dbAppendOnly |
You can add records to the recordset. You cannot update or delete existing records. This option applies only to recordsets created with the dbOpenDynaset type. |
|
dbConsistent |
On a joined recordset, only fields that do not affect the join condition can be updated. This option applies only to recordsets created with the dbOpenDynaset type. |
|
dbInconsistent |
On a joined recordset, all fields including those responsible for the join can be up dated. This option applies only to recordsets created with the dbOpenDynaset type. |
|
dbForwardOnly |
A snapshot-type recordset is created, which supports only the MoveNext record movement method. |
|
dbSQLPassThrough |
If you are using an ODBC database (for example, SQL Server or Oracle), the SQL expression used to created the recordset is executed on the server instead of using the Access database engine. |
|
dbSeeChange |
Use this option to cause a trappable error if another user tries to make a change to the record currently being edited. |
The following examples use the OpenRecordset equivalents in place of the older-style CreateDynaset, CreateSnapshot, and OpenTable methods:
Dim wstitle As Workspace Dim dbbiblio As Database Dim rstitles As Recordset Dim dstitles As Dynaset Dim sntitles As Snapshot Dim tbltitles As Table Set wstitle = DBEngine.CreateWorkspace("title", "Admin", "") Set dbbiblio = wstitle.OpenDatabase("c:\vb40\biblio.mdb", False, True) ' old method to create a Dynaset Object Set dstitles = dbbiblio.CreateDynaset("Select * from titles") 'New Method to Open a Dynaset Type Recordset Set rstitles = dbbiblio.OpenRecordset(" select * from titles", dbOpenDynaset) ' old method to create a Table Object Set tbltitles = dbbiblio.OpenTable("titles") 'New Method to Open a Dynaset Type Recordset Set rstitles = dbbiblio.OpenRecordset("titles", dbOpenTable) ' Old method to create a SnapShot Object Set sntitles = dbbiblio.CreateSnapshot("Select * from titles") 'New Method to Open a Dynaset Type Recordset Set rstitles = dbbiblio.OpenRecordset(" select * from titles", _dbOpenSnapShot)
Parameter |
Description |
Name |
The Name parameter provides identification for the new property. |
Type |
The Type parameter identifies the type of data to be stored by new property. |
Value |
The Value parameter provides the initial value of this property when the property object is created. |
FDDL |
The value of this parameter indicates whether the property being created is a Data Definition Language object. If this value is set to TRUE, the object cannot be modified or deleted unless the user has dbSecWrite definition. |
The following example adds a Boolean Dirty property to the Database object and sets its default value to TRUE:
Dim wstitle As Workspace Dim dbbiblio As Database Dim propdirty As Property Set wstitle = DBEngine.CreateWorkspace("title", "Admin", "") Set dbbiblio = wstitle.OpenDatabase("c:\vb40\biblio.mdb", False, False) ' Create the New Property Set propdirty = dbbiblio.CreateProperty("dirty", dbBoolean, True, False) ' Save the Property definition dbbiblio.Properties.Append propdirty Debug.Print propdirty.Value propdirty.Value = false Debug.Print propdirty.Value
Parameter |
Description |
Name |
The Name parameter provides the name of the newly created QueryDef. It is the name that is used in all other references to the QueryDef. |
SQLtext |
The SQLtext parameter contains the SQL statement to be executed when the QueryDef is run. |
The following sample code creates a new QueryDef and uses the newly created QueryDef to create a recordset:
Dim wstitle As Workspace Dim dbbiblio As Database Dim qrynew As QueryDef Dim rstitles As Recordset Set wstitle = DBEngine.CreateWorkspace("title", "Admin", "") Set dbbiblio = wstitle.OpenDatabase("c:\vb40\biblio.mdb", False, False) ' Create a New QueryDef Set qrynew = dbbiblio.CreateQueryDef("new", "Select * from titles where pubid _= 13") ' Load The Recordset based on the New Query Set rstitles = dbbiblio.OpenRecordset("new", dbOpenDynaset)
Parameter |
Description | |
Name |
The Name parameter provides the name of the newly created Relation object. It is the name used in all other references to the Relation object. | |
Table |
The Table parameter represents the parent table in the relationship. | |
ForeignTable |
The ForeignTable parameter represents the child table in the relationship. | |
Attributes |
The Attributes parameter further defines the characteristics of the relationship you are creating. The following constants are available for use with the Attributes parameter (you can combine options by adding them together, using the + sign between constants): | |
|
dbRelationUnique |
Indicates to the Jet engine that there is a one-to-one relationship between the parent and child tables. That is, there cannot be multiple records in the child table that relate to only one record in the parent table. |
|
dbRelationDontEnforce |
Indicates to the Jet engine that there is no referential integrity between the two tables. |
|
dbRelationInherited |
This type of relationship applies only to attached tables. It indicates to the Jet engine that the relationship integrity is maintained externally. |
|
dbRelationLeft |
Indicates to the Jet engine that for every record in the parent table there can be multiple records in the child table. |
|
dbRelationRight |
Indicates to the Jet engine that for every record in the child table there can be multiple records in the parent table. |
|
dbRelationUpdateCascade |
Indicates to the Jet engine that when the value of a field relating a parent table to a child table changes, the changes should also be made to all records in the child table. |
|
dbRelationDeleteCascade |
Indicates to the Jet engine that when a record in the parent table is deleted, all the records in the child table should also be deleted. |
The following sample code creates a new Relation object between the Publishers table and the Titles table in the BIBLIO.MDB database. The relationship is based on the PUDID field present in both tables. This relationship allows multiple titles per
publisher and supports cascaded updates and deletions.
Dim wstitle As Workspace Dim dbbiblio As Database Dim relnew As Relation Dim fld1 As Field Set wstitle = DBEngine.CreateWorkspace("title", "Admin", "") Set dbbiblio = wstitle.OpenDatabase("c:\vb40\biblio.mdb", False, False) ' Create a New Relation Set relnew = dbbiblio.CreateRelation("new", "publishers", "titles", _ dbRelationLeft + dbRelationUpdateCascade + dbRelationDeleteCascade) ' Set the parent table field for the Relation Set fld1 = relnew.CreateField("pubid") ' Set the Child Table field for the Relation fld1.ForeignName = "pubid" ' Update the Fields portion of the Relations Collection relnew.Fields.Append fld1 ' Update the New Relation to the Relations Collection dbbiblio.Relations.Append relnew
Parameter |
Description | |
Name |
The Name parameter indicates the name of the newly created table. | |
Attributes |
The Attributes parameter further defines the characteristics of the table you are creating. The following constants are available for use with the Attributes parameter: | |
|
dbAttachExclusive |
Indicates that the table is an attached table and can be opened only for exclusive use. |
|
dbAttachSavePWD |
Indicates that the user ID and password for the attached table should be saved for use in subsequent access to the table. |
|
dbSystemObject |
Indicates that the table is a System table provided by the Jet engine. Tables of this type can be opened only as read-only. |
|
dbHiddenObject |
Indicates that the table is a Hidden table provided by the Jet engine. Tables of this type are temporary tables and can be opened only as read-only. |
|
dbAttachedTable |
Indicates that the table is an attached table from a non-ODBC source. |
|
dbAttachedODBC |
Indicates that the table is an attached table from an ODBC source. |
SourceTableName |
If you are connecting an external table to the current Database object, this parameter should contain the actual name of the table in the external source. | |
Connect |
The Connect parameter is needed only when you are accessing non-Microsoft Access databases. The needs of the Connect property are specific to the type of database to which you are connecting. Further information about the Connect string for ODBC types of databases is supplied with the ODBC database driver. |
The following sample code adds the table Books to the BIBLIO.MDB database. The table contains two fields: Title and Author.
Dim wstitle As Workspace Dim dbbiblio As Database Dim newtable As TableDef Dim fld1 As Field Dim fld2 As Field Set wstitle = DBEngine.CreateWorkspace("title", "Admin", "") Set dbbiblio = wstitle.OpenDatabase("c:\vb40\biblio.mdb", False, False) ' creates a table if Data Base specified in Parameter 1 Set newtable = dbbiblio.CreateTableDef("books") ' add the fields to the tabledef Object Set fld1 = newtable.CreateField("") fld1.Name = "Title" fld1.Type = dbText fld1.Size = 30 newtable.Fields.Append fld1 Set fld2 = newtable.CreateField("") fld2.Name = "Author" fld2.Type = dbText fld2.Size = 30 newtable.Fields.Append fld2 ' Now add the table definition to the database object ' —————————————————————— dbbiblio.TableDefs.Append newtable
Parameter |
Description | |
Source |
Can be a non-record-returning SQL statement or non-record-returning QueryDef. | |
Options |
The Options parameter further defines the characteristics of the SQL or QueryDef you are running. The following constants are available for use with the Options parameter: | |
|
dbDenyWrite |
This option does not allow other users to make changes to the affected records until the Execute statement is completed. |
|
dbInconsistent |
This option allows changes to fields in tables that have been joined in a relationship. |
|
dbConsistent |
This option disallows changes to fields in tables that have been joined in a relationship. |
|
dbSQLPassThrough |
This option indicates to the Jet engine that the QueryDef or the SQL text should be passed on to the ODBC database for processing there. |
|
dbFailonError |
This option specifies that if there is an error at some point during the Execute statement, a RollBack of all changes automatically occurs. |
|
dbSeeChanges |
This option causes an error to be generated if another user is editing a record that is to be affected by the QueryDef or SQL text statement. |
The following sample code resets the Notes field in the Titles table to the comment This is a Test. If you do not want to make the changes permanent, select the No option from the message box and all the changes are rolled back.
Dim wstitle As Workspace Dim dbbiblio As Database Dim rstitles As Recordset Dim csql As String Set wstitle = DBEngine.CreateWorkspace("title", "Admin", "") Set dbbiblio = wstitle.OpenDatabase("c:\vb40\biblio.mdb", False, False) Set rstitles = dbbiblio.OpenRecordset(" select * from titles", dbOpenDynaset) wstitle.BeginTrans csql = " update titles set notes = 'Test Notes' " dbbiblio.Execute csql, dbFailOnError 'the following statement must be entered on one line If MsgBox("There have been " & dbbiblio.RecordsAffected & "Records changed_ Do you wish to Save the Changes you have Made ", vbYesNo, "", "", 0) = vbYes Then wstitle.CommitTrans Else wstitle.Rollback End If
dim nrows as long csql = " update titles set notes = 'Test Notes' " nrows = dbbiblio.ExecuteSQL(csql)
The TableDef object allows you to manipulate the structure of the tables in the database. The TableDef object contains and defines the Field and Index objects and collections.
The following properties are available to the TableDef object/collection:
The following methods are available to the TableDef object/collection:
Parameter |
Description | |
Name |
The name associated with the field. | |
Type |
The type of field you are creating. The following constants are available for use with the Type parameter. The size of the field can be automatically determined by the type of the field you are creating: | |
|
dbDate |
Indicates that the field will contain date/time information (default size: 8). |
|
dbText |
Indicates that the field can contain any type of character with a maximum length of 255 characters (default size: 1 to 255). |
|
dbMemo |
Indicates that the field can contain any character or an OLE object up to a maximum size of 1.2 gigabytes (default size: 0) |
|
dbBoolean |
Indicates that the field can contain TRUE/ FALSE or Yes/No values (default size: 1). |
|
dbInteger |
Indicates that the field can contain whole numbers in the range of —32,768 to 32,767. Additionally, an integer field can be used to contain Boolean data (default size: 2). |
|
dbLong |
Indicates that the field can contain whole numbers in the range —2,147,483,648 to 2,147,483,647 (default size: 4). |
|
dbCurrency |
Indicates that the field can contain currency type information. The maximum size of the value can be 11 numbers to the left of the decimal and 4 numbers to the right of the decimal. The number of decimals is fixed for each value in this type of field (default size: 8). |
|
dbSingle |
Indicates that the field can contain single precision floating-point numbers (default size: 4). |
|
dbDouble |
Indicates that the field can contain double-precision floating-point numbers (default size: 8). |
|
dbByte |
Indicates that the field can contain positive integers in the range 0 to 255 (default size: 1). |
|
dbLongBinary |
Indicates that the field can contain OLE objects (default size: 0). |
The following sample code creates a field named Month in the Titles table:
Dim wstitle As Workspace Dim dbbiblio As Database Dim tbltitles As TableDef Dim fld1 As Field Set wstitle = DBEngine.CreateWorkspace("title", "Admin", "") Set dbbiblio = wstitle.OpenDatabase("c:\vb40\biblio.mdb", False, False) ' Get the Existing Table Definition Set tbltitles = dbbiblio!Titles ' Create Field object. Set fld1 = tbltitles.CreateField("month", dbText, 12) ' Append MyField to Fields collection. tbltitles.Fields.Append fld1
CreateIndex. The CreateIndex method is used to create a new Index object. The CreateIndex method has only one parameter: Name, which defines the name used to refer to the index.
The following sample code creates an index on the newly created Month field in the Titles table:
Dim wstitle As Workspace Dim dbbiblio As Database Dim indx1 As Index Dim fld1 As Field Dim tbltitles As TableDef ' Create the Workspace Set wstitle = DBEngine.CreateWorkspace("title", "Admin", "") ' Open the Database Set dbbiblio = wstitle.OpenDatabase("c:\vb40\biblio.mdb", False, False) ' Open the Table Definition Set tbltitles = dbbiblio!Titles ' Create the new Index object. Set indx1 = tbltitles.CreateIndex("nonthindx") ' define the index field Set fld1 = indx1.CreateField("month") ' Define the index as non primary, non unique, and non required indx1.Unique = False indx1.Primary = False indx1.Required = False indx1.Fields.Append fld1 ' Save Index definition by appending it to Indexes _collection. tbltitles.Indexes.Append indx1
Parameter |
Description |
Name |
The Name parameter provides identification for the property being created. |
Type |
The Type parameter identifies the type of data to be stored by the property being created. |
Value |
The Value parameter provides the initial value of the property being created. |
FDDL |
The value of this parameter indicates whether the property being added is a Data Definition Language object. If this value is set to TRUE, the object cannot be modified or deleted unless the user has dbSecWrite definition. |
The following sample code adds a Contents property using the CreateProperty method to the Title TableDef object and sets its default value to Test:
Dim wstitle As Workspace Dim dbbiblio As Database Dim proptext As Property Dim tbltitle As TableDef Set wstitle = DBEngine.CreateWorkspace("title", "Admin", "") Set dbbiblio = wstitle.OpenDatabase("c:\vb40\biblio.mdb", False, False) Set tbltitle = dbbiblio!titles ' Create the New Property Set proptext = tbltitle.CreateProperty("contents", dbText, "Test", False) ' Save the Property definition tbltitle.Properties.Append proptext Debug.Print proptext.Value proptext.Value = " testing property" Debug.Print proptext.Value
Parameter |
Description | |
Type |
The Type parameter indicates the type of recordset you want to open. The following constants are available for use with the Type parameter: | |
|
dbOpenTable |
A table-type recordset is created. |
|
dbOpenDynaset |
A Dynaset-type recordset is created. |
|
dbOpenSnapShot |
A snapshot-type (read-only Dynaset) recordsetis created. |
|
The default type of recordset to open is a table-type recordset. | |
Options |
The Options parameter further defines the characteristics of the recordset you are creating. The following constants are available for use with the Options parameter. You can combine options by adding them together, using the + sign between constants. | |
|
dbDenyWrite |
Other users cannot make changes to the records contained in your recordset. |
|
dbDenyread |
Other users cannot read any of the records contained your recordset. This option applies only to recordsets created with the dbOpenTable type. |
|
dbReadOnly |
The recordset is opened as read-only; no changes can be made. |
|
dbAppendOnly |
You can add records to the recordset; you cannot update or delete existing records. This option applies only to recordsets created with the dbOpenDynaset type. |
|
dbConsistent |
On a joined recordset, only fields that do not affect the join condition can be updated. This option apples only to record sets created with the dbOpenDynaset type. |
|
dbInconsistent |
On a joined recordset, all fields including those responsible for the join can be updated. This option apples only to recordsets created with the dbOpenDynaset type. |
|
dbForwardOnly |
A snapshot-type recordset is created that supports only the MoveNext record movement method. |
|
dbSQLPassThrough |
If you are using an ODBC database (such as SQL Server or Oracle), the SQL expression used to create the recordset is executed on the server instead of using the Access database engine. |
|
dbSeeChange |
This option causes a trappable error if another user makes a change to the record currently being edited. |
The following sample code creates a recordset from the TableDef of the Titles table:
Dim wstitle As Workspace Dim dbbiblio As Database Dim proptext As Property Dim tbltitle As TableDef Dim rstitle As Recordset ' open the Workspace Set wstitle = DBEngine.CreateWorkspace("title", "Admin", "") ' open the Database Set dbbiblio = wstitle.OpenDatabase("c:\vb40\biblio.mdb", False, False) ' Set the Table Definition Set tbltitle = dbbiblio!titles ' Open the RecordSet Set rstitle = tbltitle.OpenRecordset(dbOpenDynaset)
The Field object allows you to directly manipulate the data stored in the database. It is through the Field object that data is read and placed in fields.
The following properties are available to the Field object/collection:
Constant |
Description |
dbFixedField |
Indicates that the length of the field is fixed. This is the default setting for all numeric fields. |
dbVariableField |
Indicates that the length of a field is variable. This is the default setting for all text fields. |
dbAutoIncrField |
Indicates that the field is numeric. This field cannot be changed and contains a value that is automatically assigned by the Jet engine. This type of field is supported only by Microsoft Access databases. |
dbUpdateableField |
Indicates that the value in the field can be changed. |
dbDescending |
If the field is part of an Index collection, this attribute indicates that the field is sorted in descending order. |
Constant |
Description |
dbDate |
Indicates that this field will contain date/time information (default size: 8). |
dbText |
Indicates that this field can contain any type of character with a maximum length of 255 characters (default size: 1 to 255). |
dbMemo |
Indicates that the field can contain any character or an OLE object up to a maximum size of 1.2 gigabytes (default size: 0). |
dbBoolean |
Indicates that the field can contains TRUE/FALSE or Yes/No values (default size: 1). |
dbInteger |
Indicates that the field can contain whole numbers in the range —32,768 to 32,767. Note that an integer field can be used to contain Boolean data as well (default size: 2). |
dbLong |
Indicates that the field can contain whole numbers in the range —2,147,483,648 to 2,147,483,647 (default size: 4). |
dbCurrency |
Indicates that the field can contain currency-type information. The maximum size of the value can be 11 numbers to the left of the decimal and 4 numbers to the right of the decimal. The number of decimals is fixed for each value in this type of field (default size: 8). |
dbSingle |
Indicates that the field can contain single-precision floating-point numbers (default size: 4). |
dbDouble |
Indicates that the field can contain double-precision floating-point numbers (default size: 8). |
dbByte |
Indicates that the field can contain positive integers in the range 0 to 255 (default size: 1). |
dbLongBinary |
Indicates that the field can contain OLE objects (default size: 0). |
The following example shows how you can view the properties of all of the fields contained in the BIBLIO.MDB database supplied with Visual Basic:
Control |
Name |
Caption |
Top |
Left |
Width |
Height |
FORM |
Form1 |
Bound Form |
1240 |
1635 |
6495 |
5190 |
Label |
ldlfieldname |
Field Name |
240 |
180 |
1215 |
285 |
Label |
lblfieldtype |
Field Type |
240 |
3300 |
1215 |
285 |
Label |
lblposition |
Position |
720 |
180 |
1215 |
285 |
Label |
lblsize |
Size |
720 |
3300 |
1215 |
285 |
Label |
lslsourcefield |
Source Field |
1200 |
180 |
1215 |
285 |
Label |
lblsourcetable |
Source Table |
1200 |
3300 |
1215 |
285 |
Label |
lblvalue |
Value |
1680 |
180 |
1215 |
285 |
Label |
lblattributes |
Attributes |
2100 |
180 |
1215 |
285 |
Label |
lblcollating |
Collating Order |
2100 |
3300 |
1215 |
285 |
Label |
lbldefault |
Default Value |
2520 |
180 |
1215 |
285 |
Label |
lblvalidaterule |
Validate Rule |
180 |
5040 |
1215 |
285 |
Label |
lblvalidatetext |
Validation Text |
2940 |
1560 |
1215 |
285 |
CheckBox |
chkdataupdateable |
Upateable |
3540 |
180 |
1755 |
285 |
CheckBox |
chkrequired |
Required |
3540 |
2160 |
1335 |
285 |
CheckBox |
chkallowzerolength |
Allow Zero Length |
3540 |
3780 |
1875 |
285 |
CheckBox |
chkvalidate |
Validate on Set |
3900 |
180 |
1875 |
285 |
Command |
cmdend(1) |
E&xit |
4320 |
5440 |
795 |
295 |
TextBox |
txtname |
|
240 |
1680 |
1515 |
295 |
TextBox |
txttype |
|
240 |
4740 |
1515 |
295 |
TextBox |
txtordinalposition |
|
720 |
1680 |
1515 |
295 |
TextBox |
txtsize |
|
720 |
4740 |
1515 |
295 |
TextBox |
txtsourcefield |
|
1200 |
1680 |
1515 |
295 |
TextBox |
txtsourcetable |
|
1200 |
4740 |
1515 |
295 |
Textbox |
txtvalue |
|
1680 |
1680 |
4635 |
295 |
TextBox |
txtattributes |
|
2100 |
1680 |
1515 |
295 |
TextBox |
txtcollatingorder |
|
2100 |
4740 |
1515 |
295 |
TextBox |
txtdefaultvalue |
|
2520 |
1680 |
2355 |
295 |
TextBox |
txtvalidaterule |
|
2940 |
1680 |
1515 |
295 |
TextBox |
txtvalidationtext |
|
2940 |
4740 |
1515 |
295 |
Control |
Name |
Caption |
Top |
Left |
Width |
Height |
Frame |
frame1 |
|
0 |
0 |
6735 |
4755 |
Label |
lbltable |
Table |
660 |
240 |
1215 |
295 |
Label |
lblfield |
Field |
1740 |
240 |
1215 |
295 |
Combo Box |
comtable |
|
660 |
2880 |
1875 |
300 |
Combo Box |
comfield |
|
1740 |
2880 |
1875 |
300 |
Command |
cmdend(0) |
E&xit |
4320 |
5460 |
795 |
295 |
Command |
cmdpoperties |
&Properties |
4320 |
4380 |
795 |
295 |
Figure 28.3. Properties of the selected field.
The following methods are available to the Field object/collection:
Parameter |
Description |
Name |
The value of the Name parameter provides identification for the new property. |
Type |
The Type parameter identifies the type of data to be stored by the new property. |
Value |
The Value parameter provides the initial value of the property you are adding. |
FDDL |
The value of this parameter indicates whether the property being added is a Data Definition Language object. If this value is TRUE, the object cannot be modified or deleted unless the user has dbSecWrite definition. |
The Index object contains all the information relating to indexes that exist for tables in the database. The Index object contains and defines the Field objects and collections.
The following properties apply to the Index object/collection:
The following methods apply to the Index object/collection:
In the following sample code, an Index object is created for the Titles table using the Month field:
Dim wstitle As Workspace Dim dbbiblio As Database Dim indx1 As Index Dim fld1 As Field Dim tbltitles As TableDef ' Create the Workspace Set wstitle = DBEngine.CreateWorkspace("title", "Admin", "") ' Open the Database Set dbbiblio = wstitle.OpenDatabase("c:\vb40\biblio.mdb", False, False) ' Open the Table Definition Set tbltitles = dbbiblio!Titles ' Create the new Index object. Set indx1 = tbltitles.CreateIndex("nonthindx") ' define the index field Set fld1 = indx1.CreateField("month") ' Define the index as non primary, non unique, and non required indx1.Unique = False indx1.Primary = False indx1.Required = False indx1.Fields.Append fld1 ' Save Index definition by appending it to Indexes collection. tbltitles.Indexes.Append indx1
Parameter |
Description |
Name |
The value of the Name parameter provides identification for the property you are creating. |
Type |
The Type parameter identifies the type of data to be stored by the property you are creating. |
Value |
The Value parameter provides the initial value of the property you are creating. |
FDDL |
The value of this parameter indicates whether the property being created is a Data Definition Language object. If this value is set to TRUE, the object cannot be modified or deleted unless the user has dbSecWrite definition. |
The QueryDef object allows you to manipulate the queries stored in your database. The QueryDef object contains and defines the Field and Parameter objects and collections.
The following properties are available to the QueryDef object/collection:
Constant |
Description |
dbQSelect |
The query is to select a number of records and return them to the application with no changes. |
dbQAction |
The query is to perform an action on the underlying recordset. The action may be to add a record update record or delete records. An action query does not return rows. |
dbQCrosstab |
The query is not to alter any of the data in the underlying recordset; it does, however, return summary data. |
dbQDelete |
The query is to perform a delete action. A delete query does not return any records. |
dbQUpdate |
The query is to perform an update on records in the under-lying recordset. An update query does not return any records. |
dbQAppend |
The query is to add records to the underlying recordset. An append query does not return any records. |
dbQMakeTable |
The query is to create a new table definition based on a table definition currently stored in the recordset. A make table query does not return any records. |
dbQDDL |
The query is used to make changes to the structure or index of an existing table. A DDL query does not return any records. |
dbQSQLPassThrough |
The query is not processed by the Jet engine; instead, all processing is passed on to the database server. If a passthrough query returns records to the application, the records returned are read-only. |
dbQSetOperation |
The query is to return records from multiple tables based on a join condition. The returned recordset is read-only. |
dbQSPTBulk |
This option is used in conjunction with the PassThrough method to indicate that the query will not return any records. |
The following methods are available to the QueryDef object/collection:
Parameter |
Description |
Name |
The value of the Name parameter provides identification for the property you are creating. |
Type |
The Type parameter identifies the type of data to be stored by the property you are creating. |
Value |
The Value parameter provides the initial value of the property you are creating. |
FDDL |
The value of this parameter indicates whether the property being added is a Data Definition Language object. If this value is set to TRUE, the object cannot be modified or deleted unless the user has dbSecWrite definition. |
Constant |
Description |
dbDenyWrite |
This option does not allow other uses to make changes to the affected records until the Execute statement is complete. |
dbInconsistent |
This option allows changes to fields in tables that have been joined in a relation. |
dbConsistent |
This option disallows changes to fields in tables that have been joined in a relation. |
dbSQLPassThrough |
This option indicates to the Jet engine that the QueryDef or SQL text should be passed on to the ODBC database for processing there. |
dbFailonError |
This option specifies that if an error occurs during the Execute statement, a roll back of all changes will automatically occur. |
dbSeeChanges |
This option causes an error to be generated if another user is editing a record to be affected by the QueryDef or SQL text statement. |
The following sample code resets the Notes field in the Titles table to the comment This is a Test. If you do not want to make the changes permanent, select the No option from the message box; all the changes will be rolled back.
Dim wstitle As Workspace Dim dbbiblio As Database Dim Qrynew as QUERYDEF Dim rstitles As Recordset Set wstitle = DBEngine.CreateWorkspace("title", "Admin", "") Set dbbiblio = wstitle.OpenDatabase("c:\vb40\biblio.mdb", False, False) Set rstitles = dbbiblio.OpenRecordset(" select * from titles", dbOpenDynaset) Set qrynew = dbbiblio.OpenQueryDef("new") qrynew.sql= "update titles set notes = 'Test Notes'" wstitle.BeginTrans qrynew.Execute dbfailonError If MsgBox("There have been " & qrynew.RecordsAffected & " Records changed Do you wish to Save the Changes you have Made ", vbYesNo, "", "", 0) = vbYes Then wstitle.CommitTrans Else wstitle.Rollback End If
Parameter |
Description | |
Type |
The Type parameter specifies the type of recordset you want to open. The following constants are available for use with the Type parameter: | |
|
dbOpenTable |
A table-type recordset is created. |
|
dbOpenDynaset |
A Dynaset-type recordset is created. |
|
dbOpenSnapShot |
A snapshot-type (read-only Dynaset) recordset is created. |
|
The default type of recordset to open is a table-type recordset. | |
Options |
The Options parameter further defines the characteristics of the recordset you are creating. The following constants are available for use with the Options parameter (you can combine the available options by adding them together, using the + sign between constants). | |
|
dbDenyWrite |
Other users cannot make changes to the records contained in your recordset. |
|
dbDenyread |
Other users cannot read any of the records contained your recordset. (This option apples only to recordsets created with the dbOpenTable type.) |
|
dbReadOnly |
The recordset is opened as read-only and no changes can be made. |
|
dbAppendOnly |
You can add records to the recordset; however, you cannot update or delete existing records. (This option apples only to recordsets created with the dbOpenDynaset type.) |
|
dbConsistent |
On a joined recordset, only fields that do not affect the join condition can be updated. (This option apples only to recordsets created with the dbOpenDynaset type.) |
|
dbInconsistent |
On a joined recordset, all fields including those responsible for the join can be up dated. (This option apples only to recordsets created with the dbOpenDynaset type.) |
|
dbForwardOnly |
A snapshot-type recordset is created that supports only the MoveNext record-movement method. |
|
dbSQLPassThrough |
If you are using an ODBC database (such as SQL Server or Oracle), the SQL expression used to created the recordset is executed on the server instead of using the Access database engine. |
|
dbSeeChange |
This option causes a trappable error if another user is making a change to the record currently being edited. |
The following sample code creates a recordset based on a QueryDef:
Dim wstitle As Workspace Dim dbbiblio As Database Dim proptext As Property Dim tbltitle As TableDef Dim qrynew As QueryDef Dim rstitle As Recordset ' open the Workspace Set wstitle = DBEngine.CreateWorkspace("title", "Admin", "") ' open the Database Set dbbiblio = wstitle.OpenDatabase("c:\vb40\biblio.mdb", False, False) ' Set the QueryDef Definition Set qrynew = dbbiblio.OpenQueryDef("new") qrynew.SQL = " Select * from titles " ' Open the RecordSet Set rstitle = qrynew.OpenRecordset(dbOpenDynaset)
The Parameter object allows you to specify parameters to be used by the QueryDef object.
The following properties are available to the Property object/collection:
Constant |
Description |
dbDate |
Indicates that the field is to contain date/time information (default size: 8). |
dbText |
Indicates that the field can contain any type of character with a maximum length of 255 characters (default size: 1 to 255). |
dbMemo |
Indicates that the field can contain any character or an OLE object up to a maximum size of 1.2 gigabytes (default size: 0). |
dbBoolean |
Indicates that the field can contains TRUE/FALSE or Yes/No values (default size: 1). |
dbInteger |
Indicates that the field can contain whole numbers in the range —32,768 to 32,767. Note that an integer field can be used to contain Boolean data as well (default size: 2). |
dbLong |
Indicates that the field can contain whole numbers in the range —2,147,483,648 to 2,147,483,647 (default size: 4). |
dbCurrency |
Indicates that the field can contain currency-type information. The maximum size of the value can be 11 numbers to the left of the decimal and 4 numbers to the right of the decimal. The number of decimals is fixed for each value in this type of field (default size: 8). |
dbSingle |
Indicates that the field can contain single-precision floating-point numbers (default size: 4). |
dbDouble |
Indicates that the field can contain double-precision floating point numbers (default size: 8). |
dbByte |
Indicates that the field can contain positive integers in the range 0 to 255 (default size: 1). |
dbLongBinary |
Indicates that the field can contain OLE objects (default size: 0). |
The following method is available to the Parameter object/collection:
The Recordset collection allows you to manipulate the records contained in the tables of the Database object. The Recordset object contains and defines the Field object and collection.
The following properties are available to the Recordset object/collection.
The following example shows the use of the AbsolutePosition property to move through a recordset:
Dim wstitle As Workspace Dim dbbiblio As Database Dim rstitles As Recordset Set wstitle = DBEngine.CreateWorkspace("title", "Admin", "") Set dbbiblio = wstitle.OpenDatabase("c:\vb40\biblio.mdb", False, True) Set rstitles = dbbiblio.OpenRecordset(" select * from titles", dbOpenDynaset) rstitles.AbsolutePosition = 1
The following example generates an error at runtime:
if dstitles.BOF= true then dstitles.moveprevious endif
dim sbookmark as string ' save the Bookmark sbookmark= dtitles.bookmark ' change the record pointer dstitles.movenext ' Restore the Record pointer to the previously saved bookmark. dstitles.bookmark= sbookmark
Constant |
Description |
dbEditNone |
No editing is currently taking place on this record. |
dbEditinProgress |
The Edit method has been used on the recordset and the current record is currently being changed. |
dbEditAdd |
The AddNew method has been used on the recordset and the current record is currently being added to the recordset. |
The following sample code generates an error at runtime:
if dstitles.EOF= true then dstitles.movenext endif
Following is an example of the use of the NoMatch property:
' save the Bookmark dstitles.findfirst "pubid =13" if dstitles.nomatch= true then ' record was found else ' record was not found endif
Constant |
Description |
dbOpenTable |
Indicates a table-type recordset. |
dbOpenDynaset |
Indicates a Dynaset-type recordset. |
dbOpenSnapShot |
Indicates a snapshot-type recordset. |
The following methods are available to the Recordset object/collection:
The following example adds a new record to the Titles table. The Refresh method repositions the new record in the recordset.
' set optimistic locking rstitles.lockedit= false ' add a new record rstitles.addnew rstitles("pubid")= 6 rstitles.("title") = "New Book " ' Update the new record addnew.update ' refresh the recordset in order to reposition the new record rstitles.refresh
The following example cancels an AddNew method so that no changes are made to the recordset:
' set optimistic locking rstitles.lockedit= false ' add a new record rstitles.addnew rstitles("pubid")= 6 rstitles.("title") = "New Book " ' Cancel the Record addition addnew.Cancelupdate
The following example clones the rstitles recordset to create a new recordset based on the contents of the first:
dim rstitleclone as recordset set dstitlesclone= rstitles.clone()
The following example generates a runtime error because there is no valid record pointer:
If rstitles.RecordCount = 0 Then rstitles.DELETE End If
The following example edits an existing record:
' set optimistic locking rstitles.lockedit= false ' add a new record rstitles.edit rstitles("pubid")= 6 rstitles.("title") = "New Book " ' Update the new record addnew.update
The following example locates the first record in the recordset where pubid equals 13.
dstitle.findfirst "pubid= 13" if nomatch= false ' the record was found else ' the record was not found endif
The OpenRecordset method has the following parameters:
Parameter |
Description | |
Type |
The Type parameter specifies the type of recordset you want to open. The following constants are available for use with the Type parameter: | |
|
dbOpenTable |
A table-type recordset is created. |
|
dbOpenDynaset |
A Dynaset-type recordset is created. |
|
dbOpenSnapShot |
A snapshot-type (read-only Dynaset) recordset is created. |
|
The default type of recordset to open is a table-type recordset. | |
Options |
The Options parameter further tunes the characteristics of the recordset you are creating. The following constants are available for use with the Options parameter (you can combine the available options by adding them together, using the + sign between constants): | |
|
dbDenyWrite |
Other users cannot make any changes to the records contained in your recordset. |
|
dbDenyread |
Other users cannot read any of the records contained your recordset. (This option apples only to recordsets created with the dbOpenTable type.) |
|
dbReadOnly |
The recordset is opened as read-only; no changes can be made. |
|
dbAppendOnly |
You can add records to the recordset; however, you cannot update or delete existing records. (This option apples only to recordsets created with the dbOpenDynaset type.) |
|
dbConsistent |
On a joined recordset, only fields that do not affect the join condition can be updated. (This option apples only to recordsets created with the dbOpenDynaset type.) |
|
dbInconsistent |
On a joined recordset, all fields—including those responsible for the join—can be updated. (This option apples only to recordsets created with the dbOpenDynaset type.) |
|
dbForwardOnly |
A snapshot-type recordset is created that supports only the MoveNext record movement method. |
|
dbSQLPassThrough |
If you are using an ODBC database (such as SQL Server or Oracle), the SQL expression used to create the recordset is executed on the server instead of using the Access database engine. |
|
dbSeeChange |
This option causes a trappable error if another user is making a change to the record currently being edited. |
The following example creates a new recordset from an existing recordset:
Dim wstitle As Workspace Dim dbbiblio As Database Dim proptext As Property Dim rstitle As Recordset Dim rsnewAs Recordset ' open the Workspace Set wstitle = DBEngine.CreateWorkspace("title", "Admin", "") ' open the Database Set dbbiblio = wstitle.OpenDatabase("c:\vb40\biblio.mdb", False, False) ' Open the original recordset set rstitle= wstitle.openrecordset("select * from titles",dbOpenDynaset) ' Open the RecordSet Set rsnew = qrynew.OpenRecordset(dbOpenTable)
The Relation collection allows you to define table-level relations in your database. The
Relation object contains and defines the Field object and collection.
The following properties are available to the Relation object/collection:
Constant |
Description |
dbRelationUnique |
Indicates to the Jet engine that a one-to-one relationship exists between the parent and child tables. There cannot be multiple records in the child table related to only one record in the parent table. |
dbRelationdontEnforce |
Indicates to the Jet engine that there is no referential integrity between the two tables. |
dbRelationInherited |
This type of relationship applies only to attached tables. It indicates to the Jet engine that the relationship integrity is maintained externally. |
dbRelationLeft |
Indicates to the Jet engine that for every record in the parent table there can be multiple records in the child table. |
dbRelationRight |
Indicates to the Jet engine that for every record in the child table there can be multiple records in the parent table. |
dbRelationUpdateCascade |
Indicates to the Jet engine that when the value of a field relating a parent table to a child table changes, the changes should also be made to all records in the child table. |
dbRelationDeleteCascade |
Indicates to the Jet engine that when a record in the parent table is deleted, all the records in the child table should also be deleted. |
The following methods are available to the Relation object/collection:
The Error collection allows you to set and return error codes and error messages throughout your application.
The following properties are available to the Error object/collection:
The following methods are available to the Error object/collection:
Parameter |
Description |
Number |
The Number parameter is used to specify an error number in order to identify the error that has occurred. |
Source |
The Source parameter is an optional parameter that is used to identify the application that caused the error. |
Description |
The Description parameter is an optional parameter that is used in order to provide a text description of the error that occurred. |
HelpFile |
The HelpFile parameter is an optional parameter that is used in order to provide the name of the help file containing additional information on the error that has occurred. |
HelpContext |
The HelpContext parameter is an optional parameter that is used in order to provide a topic ID in the specified help file, where further information on the error can be found. |
This chapter covered the various properties, methods, and events of the DAO. The examples provided have shown how the DAO can be used in order to manipulate the data stored in databases, as well as the underlying structure of the database.